<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta name="referrer" content="no-referrer">
    <th:block th:include="include :: header('新增文档')"/>
    <th:block th:include="include :: editormd-css"/>
</head>
<body>
<div class="main-content">
    <form id="form-md-add" class="form-horizontal">
        <div class="row">
            <div class="col-sm-offset-3 col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">文档名称：</label>
                    <div class="col-sm-8">
                        <input name="mdName" placeholder="请输入文档名称" class="form-control" type="text"
                               maxlength="50" required>
                    </div>
                </div>
            </div>
            <div class="col-sm-offset-3 col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">显示顺序：</label>
                    <div class="col-sm-8">
                        <input class="form-control" type="text" name="sort" id="sort" required>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="markdown-body editormd-preview-container" id="custom-toc-container"></div>
            <div id="editormd-div" style="margin-left: 2%">
                <textarea id="md-doc" name="mdDoc" style="display:none;"></textarea>
                <textarea id="md-html-code" name="editormd-div-html-code" style="display:none;"></textarea>
            </div>
        </div>
    </form>
</div>

<div class="row">
    <div class="col-sm-offset-5 col-sm-10">
        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存
        </button>&nbsp;
        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭
        </button>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: editormd-js"/>
<script>
    var prefix = ctx + "system/md";

    var editor;
    $(function () {
        editor = editormd("editormd-div", {
            width: "96%",
            height: 800,
            syncScrolling: "single",
            path: ctx + "editormd/lib/",
            saveHTMLToTextarea: true,
            imageUpload: true,
            theme: "dark",
            previewTheme: "dark",
            editorTheme: "base16-dark",
            tocStartLevel: 3,
            taskList: true,
            imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],//支持接收的图片上传的格式
            imageUploadURL: ctx + 'common/uploadFile' //你的controller里为上传图片所设计的路径
        });

        $("#custom-toc-container").html();
        editor.config({
            tocContainer: "",
            tocm: false,
            tocDropdown: false
        });
    });

    $("#form-md-add").validate({
        onkeyup: false,
        rules: {
            mdName: {
                minlength: 2,
                maxlength: 50,
                remote: {
                    url: prefix + "/checkMdNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "mdName": function () {
                            return $.common.trim($("#mdName").val());
                        }
                    }
                }
            },
            sort: {
                digits: true
            },
        },
        messages: {
            "mdName": {
                remote: "文档已经存在"
            },
        },
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var data = [];
            var mdName = $("input[name='mdName']").val();
            var sort = $("input[name='sort']").val();

            var content = encodeURIComponent(editor.getMarkdown())

            data.push({"name": "mdName", "value": mdName});
            data.push({"name": "sort", "value": sort});
            data.push({"name": "content", "value": content});

            data.push({"name": "content", "value": content});
            $.operate.saveTab(prefix + "/add", data);
        }
    }
</script>
</body>
</html>